package grafica;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import birdsong.client.BirdsongClient;
import birdsong.comm.implemented.ClientComm;
import birdsong.comm.implemented.ServerComm;

/**
 * @author Patricia
 * 
 */
public class BirdsongClientPrototype extends JFrame implements BirdsongClient {
	/** Buttons */
	private JButton btnLogin;
	private JButton btnLogout;
	private JButton btnSend;
	/** Labels */
	private JLabel lblInfo;
	/** Textfields */ 
	private JTextField txtUserName;
	/** TextAreas */
	private JTextArea txaChat;
	private JTextArea txaMyMessage;
	
	private String nickName;
	
	birdsong.comm.ClientComm comm;

	/**
	 * GUI's initialization
	 */
	public BirdsongClientPrototype() {
		initComponents();
	}

	/**
	 * The method for the GUI drawing
	 */
	public void initComponents() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("PROTOTYPE");
		setResizable(false);
		setSize(400, 300);
		createPanel();
		setListeners();
		setVisible(true);
	}
	
	/**
	 * Definition of the layout and creation of the components (panel, buttons, labels, ...)
	 */
	private void createPanel(){
		/*Setting of components */
		btnLogin= new JButton("LOGIN");
		btnLogout= new JButton("LOGOUT");
		btnSend= new JButton("SEND");
		lblInfo = new JLabel();
		txtUserName= new JTextField();
		txaChat= new JTextArea();
		txaChat.setEditable(false);
		txaMyMessage= new JTextArea();		
		
		JPanel pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(3,1));
		pnl1.add(txtUserName);
		pnl1.add(btnLogin);
		pnl1.add(lblInfo);
		
		JPanel pnl2 = new JPanel();
		pnl2.setLayout(new GridLayout(3,1));
		pnl2.add(btnSend);
		pnl2.add(txaMyMessage);
		pnl2.add(btnLogout);
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,1));
		panel.add(pnl1);
		panel.add(txaChat);
		panel.add(pnl2);
		
		getContentPane().add(panel);
	}
	
	
	/**
	 * Events for the buttons listeners
	 */
	private void setListeners() {
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = txaMyMessage.getText();
				if ((text != null) && (!text.equals(""))) {
					txaMyMessage.setText("");
					//sendMessage(text);
					comm.sendBirdsongMessage(text);
				}
			}
		});
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = txtUserName.getText();
				if ((text != null) && (!text.equals(""))) {
					try {
						comm.connect("127.0.0.1", nickName);
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comm.disconnect();
			}
		});
	}

		
	@Override
	public void start(birdsong.comm.ClientComm clientComm) {
		// TODO Auto-generated method stub
		comm = clientComm;
		
	}
	
	public static void main(String[] args) {
		birdsong.comm.implemented.ClientComm cComm = new ClientComm();
		birdsong.comm.implemented.ServerComm server = new ServerComm();
		server.start();	
		BirdsongClientPrototype proto = new BirdsongClientPrototype();
		proto.start(cComm);
	}

}
